package com.forevershine.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.forevershine.business.context.ContextBuilder;
import com.forevershine.business.model.StyleDetail;
import com.forevershine.business.service.AbstractService;
import com.forevershine.business.service.impl.ConfigServiceImpl;
import com.forevershine.business.service.impl.EmployeeServiceImpl;
import com.forevershine.common.Config;
import com.forevershine.personel.model.Employee;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionContext;

import static com.forevershine.common.util.HttpRequestResponseHelper.writeJsonResponse;
import static com.forevershine.common.util.GsonConverter.fromObj;

public class ConfigAction extends BaseAction {

	private static Log log = LogFactory.getLog(ConfigAction.class);

	private StyleDetail briefVo = new StyleDetail();
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private AbstractService service;

	


	@Override
	protected String service() {
		return SUCCESS;
	}

	public void updateConfig() {
		ActionContext context = ActionContext.getContext();
		Map params = ActionContext.getContext().getParameters();
		PrintWriter pw = null;
		try {
			pw = ((HttpServletResponse) context
					.get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE))
					.getWriter();

			Employee employee = new Employee();
			if (!params.entrySet().isEmpty()) {
				employee.setEmployeeid(Integer.valueOf(((String[]) params
						.get("id"))[0].toString()));
				employee.setUsername(((String[]) params.get("username"))[0]
						.toString());
				employee.setPassword(((String[]) params.get("password"))[0]
						.toString());

			} else {
				pw.write("Error");

				return;
			}
			this.service = ContextBuilder.getContext().getInstance(
					EmployeeServiceImpl.class);
			((EmployeeServiceImpl) service).updateEmployee(employee);
			pw.write("Success");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			pw.close();
		}
	}
	
	public void addConfig() {
		ActionContext context = ActionContext.getContext();
		Map params = ActionContext.getContext().getParameters();
		PrintWriter pw = null;
		try {
			pw = ((HttpServletResponse) context
					.get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE))
					.getWriter();

			Employee employee = new Employee();
			if (!params.entrySet().isEmpty()) {
				employee.setUsername(((String[]) params.get("username"))[0]
						.toString());
				employee.setPassword(((String[]) params.get("password"))[0]
						.toString());

			} else {
				pw.write("Error");

				return;
			}
			this.service = ContextBuilder.getContext().getInstance(
					EmployeeServiceImpl.class);
			((EmployeeServiceImpl) service).addEmployee(employee);
			pw.write("Success");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pw.close();
		}
	
	}
	
	public void deleteConfig()
	{
		ActionContext context = ActionContext.getContext();
		Map params = ActionContext.getContext().getParameters();
		PrintWriter pw = null;
		try {
			pw = ((HttpServletResponse) context
					.get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE))
					.getWriter();

			Employee employee = new Employee();
			if (!params.entrySet().isEmpty()) {
				employee.setEmployeeid(Integer.valueOf(((String[]) params
						.get("id"))[0].toString()));

			} else {
				log.debug("no id passed in, couldn't delete.");
				pw.write("Error");

				return;
			}
			this.service = ContextBuilder.getContext().getInstance(
					EmployeeServiceImpl.class);
			((EmployeeServiceImpl) service).deleteEmployee(employee);
			pw.write("Success");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pw.close();
		}
	}
	
	
	public void loadConfig()
	{
		
		ActionContext context = ActionContext.getContext();
		Map params = context.getParameters();
		
		Config config = new Config();
		ArrayList result = new ArrayList<String>();
		if (!params.entrySet().isEmpty()) {
			config.setAttribute(((String[]) params.get("attribute"))[0].toString());

		} else {
			log.debug("No param 'attribute' found, couldn't load contents.");
			return;
		}
		this.service = ContextBuilder.getContext().getInstance(
				ConfigServiceImpl.class);
		((ConfigServiceImpl) service).loadConfig(config, result);
		
		writeJsonResponse(fromObj(result));
		
	}
		
}
